<?php
/**
 * @user:andy
 * @date:2021/7/10
 * @company:http://www.sjdphp.com
 * @desc:
 */

namespace app\admin\controller;


use app\common\controller\BackBaseController;
use app\common\server\Json;

class BackCurrent extends BackBaseController
{
    protected $model_id = 'current_id';
    protected $model_name = 'current_name';
    protected $model_status = 'current_status';



    public function add(){
        if($this->request->isPost()){
            $param = $this->request->param();

            ## 获取验证层
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("add")->check($param)) return Json::error($validate->getError());

            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 判断开始时间及结束时间
            $time = explode("-",$param["time_range"]);
            unset($param["time_range"]);

            ## 判断时间
            $param["start_time"] = strtotime($time[0]);
            $param["end_time"]   = strtotime(trim($time[1]));

            if($param["end_time"] < time()){
                return Json::error("结束时间不能小于当前时间");
            }
            ## 获取白名单状态
            if($param["start_time"] > time()){
                $param["current_status"] = config("project.mysql_code.table_pending");
            }else{
                $param["current_status"] = config("project.mysql_code.table_normal");
            }


            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param);

            return $res;
        }else{
            return view();
        }
    }



    public function edit(){
        if($this->request->isPost()){
            $param = $this->request->param();

            ## 获取验证层
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("edit")->check($param)) return Json::error($validate->getError());

            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 判断开始时间及结束时间
            $time = explode("-",$param["time_range"]);
            unset($param["time_range"]);

            ## 判断时间
            $param["start_time"] = strtotime($time[0]);
            $param["end_time"]   = strtotime(trim($time[1]));

            if($param["end_time"] < time()){
                return Json::error("结束时间不能小于当前时间");
            }
            ## 获取白名单状态
            if($param["start_time"] > time()){
                $param["current_status"] = config("project.mysql_code.table_pending");
            }else{
                $param["current_status"] = config("project.mysql_code.table_normal");
            }


            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param,2);

            return $res;
        }else{
            $current_id = $this->request->param("current_id",0,'intval');
            if(!empty($current_id)){
                $class = self::getBusExample($this->request->controller());
                $info  = $class::getInfo(array($this->model_id => $current_id));
                if(!empty($info)){
                    $info["time_range"] = date("Y:m:d H:i:s",$info["start_time"]) . '-' .date("Y:m:d H:i:s",$info["end_time"]);
                }
            }

            return view('',[
                'info' => $info
            ]);
        }
    }

}